# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=p11-kit
pkgname=('p11-kit' 'libp11-kit' 'libp11-kit-devel')
pkgver=0.23.1
pkgrel=2
pkgdesc="Library to work with PKCS#11 modules"
arch=(i686 x86_64)
url="http://p11-glue.freedesktop.org"
license=('BSD')
makedepends=('gettext-devel' 'libiconv-devel' 'libtasn1-devel' 'libffi-devel')
# options=('debug' '!strip')
source=(${url}/releases/${pkgname}-${pkgver}.tar.gz{,.sig}
        0.19.1-trust-shrext.patch
        p11-kit-fix-readlink.patch
        0.18.7-win8-x64.patch
        0.18.7-parallel-make.patch
        p11-kit-0.20.3-msysize.patch)
md5sums=('96f073270c489c9a594e1c9413f42db8'
         'SKIP'
         '4a1d9752bb1d3776b2dbc73504915912'
         '5aa990e8d3f3d7e2d6053af5ebb9fef5'
         '07e2cffc8c80173f1806a6e10d789d27'
         '57a647d0302ac3beeda5463a4d8667e9'
         '3e82adae02a89474990709783cbb5f5d')

prepare() {
  cd ${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/0.19.1-trust-shrext.patch
  patch -p1 -i ${srcdir}/p11-kit-fix-readlink.patch
  patch -p2 -i ${srcdir}/0.18.7-win8-x64.patch
  patch -p1 -i ${srcdir}/0.18.7-parallel-make.patch
  patch -p1 -i ${srcdir}/p11-kit-0.20.3-msysize.patch

  autoreconf -fi
}

build() {
  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=( --enable-debug=yes )
  else
    extra_config+=( --enable-debug=no )
  fi

  cd ${pkgname}-${pkgver}
  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --with-trust-paths=/etc/pki/ca-trust/source:/usr/share/pki/ca-trust-source \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    "${extra_config[@]}"
  make

  make DESTDIR="${srcdir}/dest" install
  install -Dm644 COPYING ${srcdir}/dest/usr/share/licenses/${pkgname}/COPYING
}

check() {
  cd ${pkgname}-${pkgver}
  #make check
}

package_p11-kit() {
  depends=('libffi' 'libp11-kit' 'libtasn1')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/lib/pkcs11
  mkdir -p ${pkgdir}/usr/share/p11-kit/modules
  cp -f ${srcdir}/dest/usr/lib/pkcs11/*trust.so ${pkgdir}/usr/lib/pkcs11/
  cp -rf ${srcdir}/dest/usr/lib/p11-kit ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/share/p11-kit/modules/*trust* ${pkgdir}/usr/share/p11-kit/modules/
  cp -rf ${srcdir}/dest/usr/share/licenses ${pkgdir}/usr/share/
}

package_libp11-kit() {
  depends=('libintl' 'libtasn1')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libp11-kit-devel() {
  pkgdesc="Libp11-kit headers and libraries"
  groups=('development')
  depends=("libp11-kit=${pkgver}")

  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
}
